package com.shanshan.crowd.service.impl;

import com.shanshan.crowd.entity.Menu;
import com.shanshan.crowd.mapper.MenuMapper;
import com.shanshan.crowd.service.api.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName MenuServiceImpl
 * @Author 山山大魔王
 * @Date 2022/7/24 19:58
 * @Version 1.0
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    MenuMapper menuMapper;

    @Override
    public List<Menu> getAll() {
        return menuMapper.selectByExample(null);
    }

    @Override
    public void saveMenu(Menu menu) {
        menuMapper.insertSelective(menu);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public void removeMenu(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveRoleMenuRelathinship(Map<String, List<Integer>> map) {
        // 1.获取 roleId 的值
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        // 2.删除旧关联关系数据
        menuMapper.deleteOldRoleMenu(roleId);
        // 3.获取 menuIdList
        List<Integer> menuIdList = map.get("menuIdArray");
        // 4.判断 authIdList 是否有效
        if (menuIdList != null && menuIdList.size() >0) {
            // 5.插入新的记录
            menuMapper.insertNewRoleMenu(roleId,menuIdList);
        }
    }
}
